package com.xiaodu.acer.phoneplay.ui.activity;


import android.support.design.widget.TextInputLayout;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.jakewharton.rxbinding2.InitialValueObservable;
import com.jakewharton.rxbinding2.view.RxView;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.xiaodu.acer.phoneplay.R;
import com.xiaodu.acer.phoneplay.bean.LoginBean;
import com.xiaodu.acer.phoneplay.di.component.AppComponent;
import com.xiaodu.acer.phoneplay.di.component.DaggerLoginComponent;
import com.xiaodu.acer.phoneplay.di.module.LoginModule;
import com.xiaodu.acer.phoneplay.presenter.LoginPresenter;
import com.xiaodu.acer.phoneplay.presenter.contract.LoginContract;
import com.xiaodu.acer.phoneplay.ui.base.BaseActivity;
import com.xiaodu.acer.phoneplay.widget.LoadingButton;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Consumer;

public class LoginActivity extends BaseActivity<LoginPresenter>
        implements LoginContract.View {

    @BindView(R.id.txt_mobi)
    EditText mTxtMobi;
    @BindView(R.id.view_mobi_wrapper)
    TextInputLayout mViewMobiWrapper;
    @BindView(R.id.txt_password)
    EditText mTxtPassword;
    @BindView(R.id.view_password_wrapper)
    TextInputLayout mViewPasswordWrapper;
    @BindView(R.id.btn_login)
    LoadingButton mBtnLogin;
    @BindView(R.id.activity_login)
    LinearLayout mActivityLogin;

    @Override
    public int setLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void setupAcitivtyComponent(AppComponent appComponent) {
        DaggerLoginComponent.builder()
                .appComponent(appComponent)
                .loginModule(new LoginModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void initView() {
        mToolBar.setTitle(R.string.login);
    }

    @Override
    public void initData() {

    }



    @Override
    public void initListener() {
        final InitialValueObservable<CharSequence> obMobi = RxTextView.textChanges(mTxtMobi);
        final InitialValueObservable<CharSequence> obPassword = RxTextView.textChanges(mTxtPassword);

        Observable.combineLatest(obMobi, obPassword, new BiFunction<CharSequence, CharSequence, Boolean>() {
            @Override
            public Boolean apply(CharSequence obMobi, CharSequence obPassword) throws Exception {
                return isPhoneValid(obMobi.toString()) && isPasswordValid(obPassword.toString());
            }
        }).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                //判断按钮是否能点击
                RxView.enabled(mBtnLogin).accept(aBoolean);
            }
        });

        //按钮的点击事件
        RxView.clicks(mBtnLogin).subscribe(new Consumer<Object>() {
            @Override
            public void accept(@NonNull Object o) throws Exception {
                mPresenter.login(mTxtMobi.getText().toString().trim(), mTxtPassword.getText().toString().trim());
            }
        });

        mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 判断手机号是多少位
     *
     * @param phone
     * @return
     */
    private boolean isPhoneValid(String phone) {
        return phone.length() == 11;
    }

    /**
     * 判断密码最少是6位
     *
     * @param pwd
     * @return
     */
    private boolean isPasswordValid(String pwd) {
        return pwd.length() >= 6;
    }

    @Override
    public void checkPhoneError() {
        mViewMobiWrapper.setError("手机号格式不正确");
    }

    @Override
    public void checkPhoneSuccess() {
        mViewMobiWrapper.setError("");
        mViewMobiWrapper.setErrorEnabled(false);
    }

    @Override
    public void loginSuccess(LoginBean bean) {
        finish();
    }

    @Override
    public void dismissLoading() {
        mBtnLogin.showButtonText();
    }

    @Override
    public void showLoading() {
        mBtnLogin.showLoading();
    }
}
